package cn.earas.htmlX.net;

import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Clients {

    private static List<CloseableHttpClient> clients = Collections.synchronizedList(new ArrayList<CloseableHttpClient>());

    public synchronized static CloseableHttpClient getHttpClient(){
        CloseableHttpClient temp = null;
        if(clients.isEmpty()){
            temp = HttpClients.createDefault();
        }else{
            temp = clients.remove(0);
        }
        return temp;
    }

    public static void putHttpClient(CloseableHttpClient temp){
        if(temp == null) return;
        if(clients.size() >= 10) {
            try {
                temp.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }else{
            clients.add(temp);
        }
    }
}
